package com.crm.controller.invoiceController;

import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;

import com.crm.dao.InvoiceStatusDAO;
import com.crm.entity.Business;
import com.crm.entity.InvoiceStatus;


@Controller
@RequestMapping("/invoiceStatus")
public class InvoiceStatusController {

	protected final Log logger = LogFactory.getLog(getClass());
	
	InvoiceStatusDAO invoiceStatusDAO=new InvoiceStatusDAO();
	
	
	@RequestMapping("/open.do")
	public ModelAndView show(ModelAndView model,HttpServletRequest request,HttpServletResponse response,HttpSession session){
		Business bus=(Business)session.getAttribute("business");
		if(bus==null)return new ModelAndView("/jsp/index.jsp");
		int busid=bus.getBusinessId();
		List<InvoiceStatus>  lop=null;
		try {
			lop=invoiceStatusDAO.getAllInv(busid);
		} catch(NullPointerException npe){
			logger.info("Null Pointer Exception Occured");
		}
		catch (Exception e) {
			e.printStackTrace();
		}
		return model=new ModelAndView("/jsp/invoiceStatus.jsp")
		.addObject("submenu", "invoice")
		.addObject("menuName","invoice")
		.addObject("opplist",lop);
	}
	@RequestMapping("/invoiceAlerts.do")
	public ModelAndView invoiceAlerts(ModelAndView model,HttpServletRequest request,HttpServletResponse response,HttpSession session){
		Business bus=(Business)session.getAttribute("business");
		if(bus==null)return new ModelAndView("/jsp/index.jsp");
		int busid=bus.getBusinessId();
		List<InvoiceStatus>  lop=null;
		try {
			lop=invoiceStatusDAO.invoiceAlert(busid);
		} catch(NullPointerException npe){
			logger.info("Null Pointer Exception Occured");
		}
		catch (Exception e) {
			e.printStackTrace();
		}
		return model=new ModelAndView("/jsp/invoiceAlert.jsp")
		.addObject("opplist",lop);
	}
	
	@RequestMapping("/update.do")
	public ModelAndView update(ModelAndView model,HttpSession session,HttpServletRequest request,HttpServletResponse Response) throws Exception{
		Business bus=(Business)session.getAttribute("business");
		if(bus==null)return new ModelAndView("/jsp/index.jsp");
		String srowCount=request.getParameter("rowCount");
		int rowCount=Integer.parseInt(srowCount);
		List<InvoiceStatus> invoicesList =new ArrayList<InvoiceStatus>();
		InvoiceStatus invoice =null;
		for(int i=1;i<=rowCount;i++){
			String sid=request.getParameter("id"+i);
			System.out.println("Id Is:"+sid);
			if (sid!=null && sid!="") {
				invoice=new InvoiceStatus();
				int id = Integer.parseInt(sid);
				//invoice = invoiceStatusDAO.findById(id, busid);
				String invoiceNo = request.getParameter("invoiceNo" + i);
				String status = request.getParameter("status" + i);
				String soDate = request.getParameter("soDate" + i);
				String dueDate = request.getParameter("dueDate" + i);
				String sAmount = request.getParameter("amount" + i);
				System.out.println("Status Is:"+status);
				invoice.setId(id);
				invoice.setInvoicenumber(invoiceNo);
				long amount=Long.parseLong(sAmount);
				invoice.setAmount(amount);
				invoice.setBusinessId(bus);
				DateFormat formatter = new SimpleDateFormat("dd-MM-yyyy");
				 if(dueDate.equals(null)){
					 invoice.setSoDueDate( formatter.parse(dueDate));
				 }
				 if(soDate.equals(null)){
					 invoice.setSoDate( formatter.parse(soDate));
				 }
				invoice.setStatus(status);
				invoicesList.add(invoice);
			}
			
		}
		invoiceStatusDAO.insertList(invoicesList);
		return model=new ModelAndView("redirect:open.do","msg","Successfully Updated The Invoices");
	}
}
